//
//  GJFTextField.m
//  Wedding
//
//  Created by 郭静芬 on 2017/1/13.
//  Copyright © 2017年 郭静芬. All rights reserved.
//

#import "GJFTextField.h"

@interface GJFTextField () <UITextFieldDelegate>


@end

@implementation GJFTextField

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.cornerRadius = 8;
        self.clipsToBounds = YES;
        self.layer.borderColor = [UIColor whiteColor].CGColor;
        self.layer.borderWidth = 1;
        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).mas_offset(15);
            make.right.mas_equalTo(self).mas_offset(-15);
            make.top.bottom.mas_equalTo(self).mas_offset(0);
        }];
    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.length + range.location > textField.text.length) {
        return NO;
    }
    NSUInteger length = textField.text.length + string.length - range.length;
    
    if ([self.textField.placeholder containsString:@"手机号"]) {
        return length <= 11;
    }
    else if ([self.textField.placeholder containsString:@"密码"]) {
        return length <= 16;
    }
    else {
        return YES;
    }
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:16 * XX];
        _textField.delegate = self;
        [self addSubview:_textField];
    }
    return _textField;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
